home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
powervww
/
pvlbox.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1998-01-05
|
22KB
|
983 lines
// ____________________________________________________
// | |
// | Project: POWER VIEW INTERFACE |
// | File: PVLBOX.CPP |
// | Compiler: WPP386 (10.6) |
// | |
// | Subject: List box implementation |
// | |
// | Author: Emil Dotchevski |
// |____________________________________________________|
//
// E-mail: zajo@geocities.com
// URL: http://www.geocities.com/SiliconValley/Bay/3577
#define uses_string
#define uses_dc
#define uses_dialog
#define uses_icons
#define uses_label
#define uses_lbox
#define uses_system
#include "PVuses.h"
#define SCROLL_AHEAD 1
static uint ___data_size;
static Tlb_item_comparator ___comparator;
//cmp functions
static int cmp_function( const Tlb_item *a, const Tlb_item *b )
{
return
strcmp(
a->data + ___data_size,
b->data + ___data_size
);
}
static int _cmp_function( const Tlb_item *a, const Tlb_item *b )
{
return ___comparator( a, b );
}
//sort functions
static int sort_function( const void *a, const void *b )
{
return
strcmp(
( *( (Tlb_item **) a ) )->data + ___data_size,
( *( (Tlb_item **) b ) )->data + ___data_size
);
}
static int _sort_function( const void *a, const void *b )
{
return ___comparator( *( (Tlb_item **) a ), *( (Tlb_item **) b ) );
}
//search functions
static int search_function( const void *a, const void *b )
{
return
strcmp(
( (Tlb_item *) a )->data + ___data_size,
( *( (Tlb_item **) b ) )->data + ___data_size
);
}
static int _search_function( const void *a, const void *b )
{
return ___comparator( (Tlb_item *) a, *( (Tlb_item **) b ) );
}
//Tlb_list publics:
Tlb_list::Tlb_list( void ):
Tlist(),
hsize( _hsize ),
hbeg_print( _hbeg_print ),
hmax_print( _hmax_print ),
hmax_size( _hmax_size ),
changed( _changed ),
overflow( _overflow )
{
items_ptr = (Tlb_arr *) MALLOC( 0x100 * sizeof( Tlb_item * ) );
scroll_ahead = SCROLL_AHEAD;
hsize = 0;
hmax_size = 0;
hbeg_print = 0;
data_size = __lsize();
overflow = 0;
changed = 1;
lb_item_killer = __litem_killer();
lb_item_comparator = __litem_comparator();
}
Tlb_list::Tlb_list( Tlb_list *_list ):
Tlist( _list ),
hsize( _list->hsize ),
hbeg_print( _hbeg_print ),
hmax_print( _hmax_print ),
hmax_size( _list->hmax_size ),
changed( _list->changed ),
overflow( _list->overflow )
{
hmax_size = 0;
hbeg_print = 0;
items_ptr = _list->items_ptr;
data_size = _list->data_size;
lb_item_killer = _list->lb_item_killer;
lb_item_comparator = _list->lb_item_comparator;
}
Tlb_list::Tlb_list( uint &v_count, uint &v_current, uint &v_beg_print, int &v_max_print,
uint &h_size, uint &h_beg_print, int &h_max_print ):
Tlist( v_count, v_current, v_beg_print, v_max_print ),
hsize( h_size ),
hbeg_print( h_beg_print ),
hmax_print( h_max_print ),
hmax_size( _hmax_size ),
changed( _changed ),
overflow( _overflow )
{
items_ptr = (Tlb_arr *)MALLOC( 0x100 * sizeof( Tlb_item * ) );
scroll_ahead = SCROLL_AHEAD;
hsize = 0;
hmax_size = 0;
hbeg_print = 0;
data_size = __lsize();
overflow = 0;
changed = 1;
lb_item_killer = __litem_killer();
lb_item_comparator = __litem_comparator();
}
Tlb_list::~Tlb_list( void )
{
if( !( options & loASSOCIATED ) )
{
clear();
if( items_ptr != NULL ) FREE( items_ptr );
}
}
void Tlb_list::clear( void )
{
if( items_ptr != NULL )
for( uint i = 0; i < vcount; i++ )
free_lb_item( (*items_ptr)[i] );
hsize = 0;
hmax_size = 0;
hbeg_print = 0;
overflow = 0;
changed = 1;
Tlist::clear();
}
uint Tlb_list::add( void *data )
{
Tlb_item *p;
int (* cmp) ( const Tlb_item *, const Tlb_item * );
int m, l, r, x;
if( overflow ) return (uint) -1;
p = alloc_lb_item( (char *) data );
if( p == NULL )
{
overflow = 1;
return (uint) -1;
}
inc_count();
if( overflow )
{
free_lb_item( p );
return (uint) -1;
}
l = vcount - 1;
if( ( vcount > 1 ) && ( options & loSORTED ) )
{
cmp = cmp_function;
if( lb_item_comparator != NULL ) cmp = _cmp_function;
___data_size = data_size;
___comparator = lb_item_comparator;
l = 0; r = vcount - 2;
while( l <= r )
{
m = ( l + r ) / 2;
x = cmp( (*items_ptr)[m], p );
if( x == 0 )
{
l = m;
break;
}
if( x < 0 ) { l = m+1; continue; }
if( x > 0 ) r = m-1;
}
if( l < ( vcount - 1 ) )
memmove( &( (*items_ptr)[l + 1] ), &( (*items_ptr)[l] ), (vcount - l - 1) * sizeof( Tlb_item * ) );
}
(*items_ptr)[l] = p;
at( l );
changed = 1;
return l;
}
void Tlb_list::ins( uint i, void *data )
{
Tlb_item *lbi;
if( overflow ) return;
if( i >= vcount )
add( data );
else
{
lbi = alloc_lb_item( (char *) data );
if( lbi == NULL )
{
overflow = 1;
return;
}
inc_count();
if( overflow ) return;
memmove( &( (*items_ptr)[i + 1] ), &( (*items_ptr)[i] ), (vcount - i - 1) * sizeof( Tlb_item * ) );
(*items_ptr)[i] = lbi;
}
changed = 1;
}
void Tlb_list::del( uint i )
{
if( ( i >= vcount ) || !vcount ) return;
free_lb_item( (*items_ptr)[i] );
memmove( &( (*items_ptr)[i] ), &( (*items_ptr)[i+1] ), (vcount - i - 1) * sizeof( Tlb_item * ) );
dec_count();
if( vcurrent >= vcount ) bottom();
overflow = 0;
changed = 1;
}
void Tlb_list::get( uint i, void *data )
{
Tlb_item *p;
if( i >= vcount ) return;
p = (*items_ptr)[i];
memmove( data, p->data, data_size );
strcpy( ( (char *) data ) + data_size, p->data+data_size );
}
void Tlb_list::put( uint i, void *data )
{
Tlb_item *n;
if( i >= vcount ) return;
n = alloc_lb_item( (char *) data );
if( n == NULL )
{
overflow = 1;
return;
}
free_lb_item( (*items_ptr)[i] );
(*items_ptr)[i] = n;
changed = 1;
}
void Tlb_list::sort( void )
{
sort_range( 0, vcount );
}
void Tlb_list::sort_range( uint from, uint how_many )
{
int (* sort_func) ( const void *, const void * );
sort_func = sort_function;
if( lb_item_comparator != NULL ) sort_func = _sort_function;
___data_size = data_size;
___comparator = lb_item_comparator;
qsort( *items_ptr + from, how_many, sizeof( Tlb_item * ), sort_func );
}
uint Tlb_list::bin_search( Tlb_item *p )
{
return bin_search_range( p, 0, vcount );
}
uint Tlb_list::bin_search_range( Tlb_item *p, uint from, uint how_many )
{
int (* search_func) ( const void *, const void * );
Tlb_item **key;
search_func = search_function;
if( lb_item_comparator != NULL ) search_func = _search_function;
___data_size = data_size;
___comparator = lb_item_comparator;
key = (Tlb_item **) bsearch( p, *items_ptr + from, how_many, sizeof( Tlb_item * ), search_func );
if( key == NULL ) return -1;
return key - *items_ptr;
}
void Tlb_list::up( void )
{
uint i, j;
if( !vcount ) return;
i = vcurrent; j = vbeg_print;
Tlist::up();
while( !enabled( vcurrent ) && vcurrent ) Tlist::up();
if( !enabled( vcurrent ) )
{
vcurrent = i;
vbeg_print = j;
}
}
void Tlb_list::down( void )
{
uint i, j;
if( !vcount ) return;
i = vcurrent; j = vbeg_print;
Tlist::down();
while( !enabled( vcurrent ) && ( vcurrent < ( vcount - 1 ) ) ) Tlist::down();
if( !enabled( vcurrent ) )
{
vcurrent = i;
vbeg_print = j;
}
}
void Tlb_list::top( void )
{
if( !vcount ) return;
Tlist::top();
while( !enabled( vcurrent ) && ( vcurrent < ( vcount - 1 ) ) ) Tlist::down();
}
void Tlb_list::bottom( void )
{
if( !vcount ) return;
Tlist::bottom();
while( !enabled( vcurrent ) && vcurrent ) Tlist::up();